package cn.itcast.gateway.filter;

import org.bouncycastle.jcajce.provider.asymmetric.rsa.RSAUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class MyFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println(exchange.getRequest().getId());
        System.out.println(exchange.getRequest().getPath());
        System.out.println(exchange.getRequest().getRemoteAddress());
        System.out.println(exchange.getResponse().getStatusCode());
        System.out.println(exchange.getApplicationContext().getId());
        System.out.println(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());

        if (exchange.getRequest().getRemoteAddress().getAddress().getHostAddress().equals("127.0.0.1")){
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
            return response.setComplete();
        }
        return chain.filter(exchange);//放行
    }

    //过滤器优先级
    @Override
    public int getOrder() {
        return 0;
    }
}
